<?php

namespace App\Models;

class CommodityActivitySkuStatistics extends Base
{
    public function commodityActivitySku()
    {
        return $this->belongsTo(CommodityActivitySku::class);
    }

    /**
     * 增加/减少 出货量
     * @param int $unitPrice 成本单价
     * @param int $quantitySold
     * @param string $method
     */
    public function incrementOrDecrementQuantitySold($unitPrice, $quantitySold = 1, $method = 'increment')
    {
        $quantitySold > 0 && $this->{$method}('quantity_sold', $quantitySold);
        $quantitySold > 0 && $this->{$method}('quantity_sold_amount', $quantitySold * $unitPrice);
    }

    /**
     * 退货总数
     * @param int $unitPrice 成本单价
     * @param int $quantity
     * @param string $method
     */
    public function incrementOrDecrementTotalReturns($unitPrice, $quantity = 1, $method = 'increment')
    {
        $quantity > 0 && $this->{$method}('total_returns', $quantity);
        $quantity > 0 && $this->{$method}('total_returns_amount', $quantity * $unitPrice);
    }

    /**
     * 退款总数
     * @param int $unitPrice 成本单价
     * @param int $quantity
     * @param string $method
     */
    public function incrementOrDecrementTotalRefunds($unitPrice, $quantity = 1, $method = 'increment')
    {
        $quantity > 0 && $this->{$method}('total_refunds', $quantity);
        $quantity > 0 && $this->{$method}('total_refunds_amount', $quantity * $unitPrice);
    }

    /**
     * 群主销售量
     * @param int $unitPrice 群主单价
     * @param int $purchasePrice 成本单价
     * @param int $groupOwnerShipments 出货数量
     * @param string $method
     */
    public function incrementOrDecrementGroupOwnerShipments($unitPrice, $purchasePrice, $groupOwnerShipments = 1, $method = 'increment')
    {
        $groupOwnerShipments > 0 && $this->{$method}('group_owner_shipments', $groupOwnerShipments);
        $groupOwnerShipments > 0 && $this->{$method}('group_owner_shipments_amount', $groupOwnerShipments * $unitPrice); // 群主总金额
        $groupOwnerShipments > 0 && $this->{$method}('group_owner_shipments_purchase_amount', $groupOwnerShipments * $purchasePrice); // 成本总金额
    }

    /**
     * 精英销售量
     * @param int $unitPrice 精英单价
     * @param int $purchasePrice 成本单价
     * @param int $eliteShipments 出货数量
     * @param string $method
     */
    public function incrementOrDecrementEliteShipments($unitPrice, $purchasePrice, $eliteShipments = 1, $method = 'increment')
    {
        $eliteShipments > 0 && $this->{$method}('elite_shipments', $eliteShipments);
        $eliteShipments > 0 && $this->{$method}('elite_shipments_amount', $eliteShipments * $unitPrice);
        $eliteShipments > 0 && $this->{$method}('elite_shipments_purchase_amount', $eliteShipments * $purchasePrice);
    }

    /**
     * 大咖销售量
     * @param int $unitPrice 大咖单价
     * @param int $purchasePrice 成本单价
     * @param int $strongmanShipments 出货数量
     * @param string $method
     */
    public function incrementOrDecrementStrongmanShipments($unitPrice, $purchasePrice, $strongmanShipments = 1, $method = 'increment')
    {
        $strongmanShipments > 0 && $this->{$method}('strongman_shipments', $strongmanShipments);
        $strongmanShipments > 0 && $this->{$method}('strongman_shipments_amount', $strongmanShipments * $unitPrice);
        $strongmanShipments > 0 && $this->{$method}('strongman_shipments_purchase_amount', $strongmanShipments * $purchasePrice);
    }

    /**
     * 邀请数量
     * @param $amount
     * @param int $invitationAmount 邀请(奖励)金额
     * @param string $type
     * @param string $method
     */
    public function incrementOrDecrementNumberOfInvitationRewards($amount, $invitationAmount, $type = 'direct', $method = 'increment')
    {
        $amount > 0 && $this->{$method}('number_of_' . $type . '_invitations', $amount);
        $amount > 0 && $this->{$method}('number_of_' . $type . '_invitations_amount', $amount * $invitationAmount);
    }

    /**
     * 增加 / 减少 佣金
     * @param $amount
     * @param $type
     * @param string $method
     */
    public function incrementOrDecrementNumberOfCommissions($amount, $commissionAmount, $type, $method = 'increment')
    {
        $amount > 0 && $this->{$method}($type, $amount);
        $amount > 0 && $this->{$method}($type . '_amount', $amount * $commissionAmount);
    }
}
